package com.zychao.smex.po;

import java.util.LinkedHashMap;
import java.util.Map;

public class Cart {
	
	private Map<String, CartItem> map = new LinkedHashMap<String, CartItem>();
	
	@SuppressWarnings("unused")
	private double allPrice;
	
	public void addToCart(Book book) {
		CartItem item = map.get(book.getId());
		if(item != null) {
			item.setCount(item.getCount() + 1);
		} else {
			item = new CartItem(); 
			item.setBook(book);
			item.setPrice(book.getPrice());
			item.setCount(1);
			map.put(book.getId(), item);
		}
	}

	public Map<String, CartItem> getMap() {
		return map;
	}

	public void setMap(Map<String, CartItem> map) {
		this.map = map;
	}
	
	public double getAllPrice() {
		
		double totalPrice = 0;
		for(Map.Entry<String, CartItem> me : map.entrySet()) {
			CartItem item = me.getValue();
			totalPrice += item.getPrice();
		}
		return totalPrice;
	}
	
	public void setAllPrice(double allPrice) {
		this.allPrice = allPrice;
	}
}
